package com.example.leero.graduation.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leero.graduation.API;
import com.example.leero.graduation.App;
import com.example.leero.graduation.BaseActivity;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.GsonUtil;
import com.example.leero.graduation.Util.LogUtil;
import com.example.leero.graduation.Util.PreferenceUtil;
import com.example.leero.graduation.Util.StringUtil;
import com.example.leero.graduation.model.User;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

import okhttp3.Call;

public class LoginActivity extends BaseActivity implements View.OnClickListener{

    private Button login;
    private TextView go_register, forget_password;
    private EditText phone, password;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(Bundle state) {
        if (App.isLogin()) {
            Intent intent = new Intent(this, HomeActivity.class);
            startActivity(intent);
        }

        go_register = (TextView) findViewById(R.id.go_register);
        go_register.setOnClickListener(this);
        forget_password = (TextView) findViewById(R.id.forget_password);
        forget_password.setOnClickListener(this);
        login = (Button) findViewById(R.id.btn_login);
        login.setOnClickListener(this);

        phone = (EditText) findViewById(R.id.phone);
        password = (EditText) findViewById(R.id.password);

        if (!StringUtil.isEmpty(App.getUserPhone())) {
            phone.setText(App.getUserPhone());
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch(v.getId()) {
            case R.id.btn_login:
                if (checkInput()) {
                    login();
                }
                break;
            case R.id.go_register:
                intent.setClass(this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.forget_password:
                intent.setClass(this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void login() {
        OkHttpUtils.post().url(API.LOGIN)
                .addParams("phone", phone.getText().toString())
                .addParams("password", password.getText().toString())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String message = jsonObject.getString("message");
                    if (!jsonObject.getBoolean("state")) {
                        Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
                    } else {
                        User user = GsonUtil.fromJson(jsonObject.getString("user"), User.class);
                        user.publish_num = Integer.parseInt(jsonObject.getString("publish_num"));
                        user.join_num = Integer.parseInt(jsonObject.getString("join_num"));
                        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                        App.login(user);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private boolean checkInput() {
        if (phone.getText().toString().length() != 11) {
            Toast.makeText(this, "请输入正确的电话号码", Toast.LENGTH_SHORT).show();
            return false;
        } else if (password.getText().toString().length() < 8) {
            Toast.makeText(this, "请输入8位以上的密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

}
